package com.wecloud.core.action.impl;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.wecloud.core.action.base.Action;
import com.wecloud.core.action.base.Confirmable;
import com.wecloud.core.action.base.Prepareable;
import com.wecloud.core.constant.WeContext;
import com.wecloud.core.data.config.BussinessClient;
import com.wecloud.core.data.jnl.JnlService;
import com.wecloud.core.exception.WeException;

public class BaseTransCfmAction extends BussinessClient implements ApplicationContextAware ,Action,Prepareable,Confirmable{
	@Autowired
	private ApplicationContext applicationContext;
	
	private boolean token;
	

	public boolean isToken() {
		return token;
	}

	public void setToken(boolean token) {
		this.token = token;
	}

	@Override
	public void prepare(WeContext context)  throws WeException{
		
	}

	@Override
	public void excute(WeContext context)  throws WeException{
		
	}
	
	public void after(WeContext context)throws WeException{
		try{
			((JnlService)applicationContext.getBean("jnlService")).getJnlService(context.getTransactionId()).prepareJnlData(context);
			((JnlService)applicationContext.getBean("jnlService")).getJnlService(context.getTransactionId()).write(context.getData());
			((JnlService)applicationContext.getBean("jnlService")).getJnlService(context.getTransactionId()).update(context.getData());
			((JnlService)applicationContext.getBean("jnlService")).getJnlService(context.getTransactionId()).delete(context.getData());
		}catch(Exception e){
			throw new WeException(e);
		}
	}
	/**
	 * token验证
	 */
	@Override
	public void confirm(WeContext context)  throws WeException{
		if(token){
//			HttpSession session = context.getRequest().getSession();
//			if(session.getAttribute("_token") == null || "".equals(session.getAttribute("_token"))){
//				throw new WeException(ErrorMsg.Error_9996);
//			}
//			String sessionToken = (String) session.getAttribute("_token");
//			String appToken = context.getString("_token");
//			if(sessionToken.equals(appToken)){
//				session.setAttribute("_token", "");
//			}else{
//				throw new WeException(ErrorMsg.Error_9996);
//			}
		}
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}
}
